﻿Imports System.Windows.Forms
Imports System.Data.OleDb

Public Class ReportDialog
    Private Function ReadDBF(FilePath As String) As Double
        'Mainform.ToolStripProgressBar1.Value = 1
        'Mainform.ToolStripProgressBar1.Style = ProgressBarStyle.Marquee
        'Mainform.ToolStripStatusLabel1.Text = "Generating Net Sales Summary of " & selecteditems.Cells(2).Value & " " & DefaultDate

        Dim Quantity As String = "QUANTITY"
        Dim DISCPRIC As String = "DISCPRIC"
        Dim INCLTAX As String = "INCLTAX"

        Dim TotalList As New ArrayList


        Dim SQLstr As String = "SELECT * FROM " & "GNDITEM"
        Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & _
            " ;Extended Properties=dBASE IV;"

        Try

            Dim DBFConn As New OleDbConnection(ConnString)
            DBFConn.Open()

            Dim DBFCommand As OleDbCommand = New OleDbCommand(SQLstr, DBFConn)
            Dim DBFDataReader As OleDbDataReader = DBFCommand.ExecuteReader(CommandBehavior.SequentialAccess)

            While DBFDataReader.Read
                TotalList.Add((DBFDataReader(Quantity) * DBFDataReader(DISCPRIC)) - DBFDataReader(INCLTAX))
            End While

            Dim Netsales As Double = 0

            For Each item As Double In TotalList
                Netsales = item + Netsales
            Next

            DBFConn.Close()
            Return Netsales
        Catch ex As Exception
            'MsgBox(ex.Message)
            Return 0
        End Try
    End Function
    Function Yesterdate() As String
        Dim intCurrentYear As Integer = Now.Year
        Dim intCurrentMonth As Integer = Now.Month
        Dim intCurrentDay As Integer = Now.Day - 1

        Dim CurrentMonth As String = intCurrentMonth
        Dim Currentday As String = intCurrentDay

        If intCurrentDay = 1 Then
            intCurrentMonth = intCurrentMonth - 1
            intCurrentDay = 30
        End If


        If intCurrentMonth < 10 Then
            CurrentMonth = "0" & intCurrentMonth
        End If

        If intCurrentDay < 10 Then
            Currentday = "0" & intCurrentDay
        End If


        Dim yesdate As String = intCurrentYear & CurrentMonth & Currentday
        Return yesdate
    End Function
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub ReportDialog_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Text = "Generate Net Sales Report : " & StoreName
        TextBox1.Focus()
        Dim SetDate As String = ""
        Dim DefaultDate As String = Yesterdate()
       
        TextBox1.Text = DefaultDate

    End Sub
    Public StoreName, ServerIP, storetype As String

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        CheckForIllegalCrossThreadCalls = False
        Dim t As New Threading.Thread(AddressOf Start)
        t.Start()
    End Sub
    Sub Start()
        ' Dim FilePath As String = My.Application.Info.DirectoryPath
        'Build Path
        Button1.Enabled = False
        Button1.Text = "Generating.."
        Try
            For Each dates As String In TextBox1.Lines
                Dim Targetpath As String = "\\" & ServerIP & "\bootdrv\" & storetype & "\" & dates

                'MsgBox(path)
                Dim Netsales As Double = ReadDBF(Targetpath)

                ListBox1.Items.Add(dates & ":" & Netsales)
            Next
        Catch ex As Exception

        End Try

        Button1.Enabled = True
        Button1.Text = "Generate"
    End Sub

End Class
